<?php
//https://code.google.com/apis/console/#project:636516852314:services
//API key:	AIzaSyDfYlODWJrm2SRdcjsZdBvVwZ3DMoRL-t8
require_once("../../Model/db_GCMUsers.cls.php");
class GCM {
 
    //put your code here
    // constructor
    function __construct() {
         
    }
 
    /**
     * Sending Push Notification
     */
    public function send_notification($registatoin_ids, $message) {
        // include config
        include_once './config.php';
 
        // Set POST variables
        $url = 'https://android.googleapis.com/gcm/send';
		
        $fields = array(
            'registration_ids' => $registatoin_ids,
            'data' => $message,
        );
 
        $headers = array(
            'Authorization: key=' . GOOGLE_API_KEY,
            'Content-Type: application/json'
        );
        // Open connection
        $ch = curl_init();
 
        // Set the url, number of POST vars, POST data
        curl_setopt($ch, CURLOPT_URL, $url);
 
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
        // Disabling SSL Certificate support temporarly
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
 
        // Execute post
        $result = curl_exec($ch);
        if ($result === FALSE) {
            die('Curl failed: ' . curl_error($ch));
        }
		
        // Close connection
        curl_close($ch);
        echo $result;
    }
	
	//
	public function registerGCM($regID, $name, $email)
	{
		$db_gcm = new db_gcm_user();
		if(empty($regID)||!isset($regID))
			return false;
		$data = array('ID'=>$regID, 'Name'=>$name,'Email'=>$email);
		$db_gcm->insert($data);
		
		$message = array('Status'=>'Success', 'Message'=> ' Đăng kí thành công! ');
		$result = $gcm->send_notification($regID, $message);
		echo $result;
	}
}

?>